package com.yndsoft.jc.ui.memberpage.login;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yndsoft.jc.R;
import com.yndsoft.jc.activity.BaseActivity;
import com.yndsoft.jc.model.ReturnMsg;
import com.yndsoft.jc.supplier.utils.AppSession;
import com.yndsoft.jc.supplier.utils.CountDownTimerUtil;
import com.yndsoft.jc.supplier.utils.IsInputWatcher;
import com.yndsoft.jc.supplier.utils.ToastUtil;
import com.yndsoft.jc.supplier.widget.dialog.SupplierDialog;
import com.yndsoft.jc.ui.net.util.ProgressSubscriber;

import butterknife.Bind;

/**
 * Created by hasee on 2017/12/6.
 */

public class ForgetPswActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.base_backBtn)
    RelativeLayout base_backBtn;
    @Bind(R.id.base_title)
    TextView base_title;

    @Bind(R.id.forg_register_phone)
    EditText forg_register_phone;
    @Bind(R.id.forg_register_del)
    RelativeLayout forg_register_del;
    @Bind(R.id.forg_identifying_code)
    EditText forg_identifying_code;
    @Bind(R.id.forg_register_send_sms)
    Button forg_register_send_sms;
    @Bind(R.id.forg_register_pwd)
    EditText forg_register_pwd;
    @Bind(R.id.forg_register_pwd_again)
    EditText forg_register_pwd_again;
    @Bind(R.id.forget_psw_completeBtn)
    Button forget_psw_completeBtn;

    private CountDownTimerUtil countDownTimer;
    private String role;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_psw);

        setViews();
        setListener();
    }

    private void setViews() {

        role = getIntent().getStringExtra("role");
        base_title.setText(getResources().getString(R.string.forget_psw));
    }

    private void setListener() {
        base_backBtn.setOnClickListener(this);
        forg_register_phone.addTextChangedListener(isInput);
        forg_register_send_sms.setOnClickListener(this);
        forget_psw_completeBtn.setOnClickListener(this);
        forg_register_del.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.base_backBtn:
                finish();
                break;
            case R.id.forg_register_del:
                forg_register_phone.setText("");
                break;
            case R.id.forg_register_send_sms:
                String phoneSend = forg_register_phone.getText().toString().trim();
                if (!TextUtils.isEmpty(phoneSend) && phoneSend.length() == 11) {
                    countDownTimer = new CountDownTimerUtil(forg_register_send_sms);
                    countDownTimer.start();
                    getMessage(phoneSend);
                } else {
                    ToastUtil.show(mContext, "请输入正确的手机号！");
                }
                break;
            case R.id.forget_psw_completeBtn:
                String phoneValue = forg_register_phone.getText().toString().trim();
                String codeValue = forg_identifying_code.getText().toString().trim();
                String pwdValue = forg_register_pwd.getText().toString();
                String pwdValueAgain = forg_register_pwd_again.getText().toString().trim();
                if (TextUtils.isEmpty(phoneValue)) {
                    showHint("手机号不能为空");
                } else {
                    if (phoneValue.length() != 11) {
                        showHint("请输入正确的手机号");
                    } else {
                        if (TextUtils.isEmpty(codeValue)) {
                            showHint("验证码不能为空");
                        } else {
                            if (codeValue.length() != 6) {
                                showHint("请输入正确的验证码！");
                            } else {
                                if (TextUtils.isEmpty(pwdValue)) {
                                    showHint("密码不能为空");
                                } else {
                                    if (pwdValue.length() < 6 || pwdValue.length() > 20) {
                                        showHint("请输入6-20位密码！");
                                    } else {
                                        if (TextUtils.isEmpty(pwdValueAgain)) {
                                            showHint("请再次输入密码");
                                        } else {
                                            if (!pwdValue.equals(pwdValueAgain)) {
                                                showHint("两次输入密码不一致");
                                            } else {
                                                if (codeValue.equals(identifyingCode)) {
                                                    //重置密码
                                                    if ("supplier".equals(role)) {
                                                        changeSupplierPwd(phoneValue, pwdValue);
                                                    } else {
                                                        resttingPsw(phoneValue, pwdValue);
                                                    }
                                                } else {
                                                    showHint("验证码错误");
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                break;
        }
    }

    private void changeSupplierPwd(String phone, String pwd) {
        mHttpUtil.forgetSupplierPwd(new ProgressSubscriber<ReturnMsg>(mContext) {
            @Override
            public void next(ReturnMsg returnMsg) {
                switch (returnMsg.getCode()) {
                    case 0:
                        ToastUtil.show(mContext, "修改失败");
                        break;
                    case 1:
                        ToastUtil.show(mContext, "修改成功");
                        finish();
                        break;
                }
            }
        }, "forgetpwd", phone, pwd);
    }

    /**
     * 重置密码
     */
    private void resttingPsw(String phone, String pwd) {
        mHttpUtil.forgetPwd(new ProgressSubscriber<ReturnMsg>(mContext) {
            @Override
            public void next(ReturnMsg returnMsg) {
                switch (returnMsg.getCode()) {
                    case 0:
                        ToastUtil.show(mContext, "修改失败");
                        break;
                    case 1:
                        ToastUtil.show(mContext, "修改成功");
                        break;
                }
            }
        }, "forgetpwd", phone, pwd);
    }

    String identifyingCode;

    private void getMessage(String phoneSend) {
        mHttpUtil.getSms(new ProgressSubscriber<ReturnMsg>(mContext) {
            @Override
            public void next(ReturnMsg returnMsg) {
                switch (returnMsg.getCode()) {
                    case 2:
                        ToastUtil.show(mContext, "该手机号不存在");
                        break;
                    case 1:
                        ToastUtil.show(mContext, "获取成功");
//                        Type type = new TypeToken<String>() {
//                        }.getType();
//                        Object fromJson = new Gson().fromJson(returnMsg.getInfo(), type);
////                        KeyValue keyValue = (KeyValue) fromJson;
//                        identifyingCode = (String) fromJson;
                        identifyingCode = returnMsg.getInfo();
                        break;
                }
            }
        }, "shortmsg", phoneSend, 2);
    }

    private void showHint(String content) {
        new SupplierDialog(mContext).show(false, "提示", content);
    }

    private IsInputWatcher isInput = new IsInputWatcher() {
        @Override
        public void noInput() {
            forg_register_del.setVisibility(View.INVISIBLE);
        }

        @Override
        public void inputting() {
            forg_register_del.setVisibility(View.VISIBLE);
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
    }
}
